<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 消息 model
 *
 */
class Fansmodel extends CI_Model {
	function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 取得用户的粉丝
     *
     * @param unknown_type $id
     */
    function get_fans($uid)
    {
    	$result = array();
    	$this->dblib->set_query_limit();
    	$this->db->select('user.id as uid, user.name, user.photo, user.reg_type, fans.createtime')
				->from('user')
				->join('fans', 'user.id=fans.fanid')
				->where('fans.uid', $uid)
				->order_by('createtime', 'desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			//如果有结果集
			$this->load->library('uploadimglib');
			$result = $query->result_array();
			//设置photo路径
			foreach ($result as $k=>$v){
				$result[$k]['photo'] = $this->uploadimglib->re_user_photo($v['photo'], $v['reg_type']);
				unset($result[$k]['reg_type']);//不需要此字段
			}
		}
		return $result;
    }
    
    /**
     * 添加粉丝（成为粉丝）
     *
     * @param unknown_type $uid
     * @param unknown_type $fanid
     */
    function insert_fans($uid='', $fanid='')
    {
    	$data = array('uid'	=> $uid, 'fanid'	=> $fanid);
		return $this->db->insert('fans', $data);
    }
    
    /**
     * 取消粉丝关系
     *
     * @param unknown_type $uid
     * @param unknown_type $fanid
     */
    function delete_fans($uid='', $fanid='')
    {
    	$this->db->limit(1);
		return $this->db->delete('fans', array('uid'=>$uid, 'fanid'=>$fanid));
	}
	
	/**
	 * 检测粉丝关系
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $fanid
	 */
	function check_fans($uid='', $fanid='')
	{
		$this->db->select('count(*) as count');
		$this->db->limit(1);
		$query = $this->db->get_where('fans', array('uid' => $uid, 'fanid' => $fanid));
		$row = $query->row_array();
		return $row['count'] > 0;
	}
    
}

/* End of file fansmodel.php */
/* Location: ./application/models/fansmodel.php */